package jp.ddo.trismegistos.timer.activity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import jp.ddo.trismegistos.timer.R;
import jp.ddo.trismegistos.timer.adpter.TimeAdpter;
import jp.ddo.trismegistos.timer.entity.Time;
import jp.ddo.trismegistos.timer.logic.CountupTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * @author y_sugasawa
 * @author $Author: aigis.developer@gmail.com $
 * @version $Rev: 41 $ $Date: 2011-09-26 15:42:24 +0000 (Mon, 26 Sep 2011) $
 * 
 */
public class Stopwatch extends Activity {

	private TextView countupView;

	private ListView listView;

	private Timer timer;

	private CountupTask countupTask;

	private Handler handler = new Handler();

	private List<Time> items = new ArrayList<Time>();

	private TimeAdpter timeAdpter;

	private static final DecimalFormat df = new DecimalFormat();

	private double fastestLapTime;

	static {
		df.applyPattern("0");
		df.setMaximumFractionDigits(3);
		df.setMinimumFractionDigits(3);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stopwatch);

		countupView = (TextView) findViewById(R.id.countupView);
		listView = (ListView) findViewById(R.id.listView);

		timeAdpter = new TimeAdpter(this, R.layout.list, items, df);
		listView.setAdapter(timeAdpter);

		setStartButtonListener();
		setStopButtonListener();
		setCheckButtonListener();
	}

	protected void setStartButtonListener() {
		final Button button = (Button) findViewById(R.id.startButton);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (timer == null) {
					items.clear();
					timeAdpter.notifyDataSetChanged();
					countupTask = new CountupTask(handler, countupView, df);
					timer = new Timer(true);
					timer.scheduleAtFixedRate(countupTask, 1, 1);
				}
			}
		});
	}

	protected void setStopButtonListener() {
		final Button button = (Button) findViewById(R.id.stopButton);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (timer != null) {
					timer.cancel();
					timer = null;
					updateListView();
				}
			}
		});
	}

	protected void setCheckButtonListener() {
		final Button button = (Button) findViewById(R.id.checkButton);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (timer != null) {
					updateListView();
				}
			}
		});
	}

	protected void updateListView() {
		final Time time = new Time();
		final double nowTime = countupTask.getTime();
		time.setNowTime(nowTime);

		double lapTime;
		if (items.isEmpty() == false) {
			lapTime = time.calcLap(items.get(items.size() - 1).getNowTime());
		} else {
			lapTime = time.calcLap(0.00d);
		}

		if (items.isEmpty() || fastestLapTime > lapTime) {
			fastestLapTime = lapTime;
		}
		items.add(time);
		updateTopFlg();
		timeAdpter.notifyDataSetChanged();
		listView.smoothScrollToPosition(items.size() - 1);
	}

	protected void updateTopFlg() {
		for (final Time time : items) {
			if (fastestLapTime == time.getLapTime()) {
				time.setTop(true);
			} else {
				time.setTop(false);
			}
		}
	}
}
